package com.liyujie.serialport.dialog;

import android.content.Context;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.EditText;
import android.widget.TextView;

import com.blankj.utilcode.util.TimeUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.cy.dialog.BaseDialog;
import com.liyujie.serialport.R;
import com.liyujie.serialport.utils.activation.Authorize;

/**
 * 激活码
 */
public class ActivationCodeDialog extends BaseDialog {
    //设备码
    private TextView DeviceCode;
    //激活码
    private EditText ActivationCode;


    public ActivationCodeDialog(Context context) {
        super(context);
    }

    public ActivationCodeDialog(Context context, int themeResId) {
        super(context, themeResId);
    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        contentView(R.layout.dialog_activation_code);
        animType(AnimInType.CENTER);
        canceledOnTouchOutside(false);
        setCancelable(false);
        //设备码
        DeviceCode = findViewById(R.id.DeviceCode);
        String EquipmentCode = TimeUtils.millis2String(TimeUtils.getNowMills());

        DeviceCode.setText("系统授权码过期，请联系管理员获取密码解锁:\n\n" + Authorize.GetDeviceNumSERIAL());
        //激活码
        ActivationCode = findViewById(R.id.ActivationCode);
        //确认激活
        findViewById(R.id.ConfirmTheActivation).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String mActivationCode = ActivationCode.getText().toString();
                if(TextUtils.isEmpty(mActivationCode)){
                    ToastUtils.getDefaultMaker().show("请输入激活码");
                    return;
                }
                //判断激活
//                boolean succeed = CommonUtils.CheckPwdPlant(EquipmentCode,mActivationCode);
                if(Authorize.Auth(mActivationCode)){//成功
                    dismiss();
                }else {
                    ToastUtils.getDefaultMaker().show("激活码错误");
                }
                ActivationCode.setText("");
            }
        });
    }

    @Override
    public void show() {
        super.show();
        Window window = getWindow();
        //设置弹出位置
        window.setGravity(Gravity.CENTER);
        //设置对话框大小
        window.setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
    }
}
